package Servlet.Teacher;

import Entity.Grade.AdviserGrade;
import Entity.MyTableData;
import Entity.Process.Medium;
import Entity.Process.ReportDetail;
import Entity.Process.Task;
import Entity.Process.TaskDetail;
import Service.Teacher.Imp.ProcessServiceImp;
import Service.Teacher.ProcessService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * Author: Deng PengFei
 * Date: 2021/8/23
 * Time: 11:10
 */

@WebServlet(urlPatterns = {"/getPaper", "/addAssignment", "/getTaskById", "/addMedium", "/addGrade", "/getReportById"},name = "ProcessServlet")
public class ProcessServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();
        if ("/getPaper".equals(url)) {
            getPaper(request, response);
        } else if ("/addAssignment".equals(url)) {
            addAssignment(request, response);
        } else if ("/getTaskById".equals(url)) {
            getTaskById(request, response);
        } else if ("/addMedium".equals(url)) {
            addMedium(request, response);
        } else if ("/addGrade".equals(url)) {
            addGrade(request, response);
        } else if ("/getReportById".equals(url)) {
            getReportById(request, response);
        }
    }

    private void getReportById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ProcessService processService = new ProcessServiceImp();
        Integer id = Integer.parseInt(request.getParameter("id"));
        ReportDetail reportDetail = processService.getReportDetailById(id);
        String json = JSONObject.toJSONString(reportDetail, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }

    /**
     * 指导老师插入一条评分
     */
    private void addGrade(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ProcessService processService = new ProcessServiceImp();
        String TNO = request.getParameter("userName");
        String gradeData = request.getParameter("gradeData");
        JSONObject jsonObject = JSONObject.parseObject(gradeData);
        String sno = jsonObject.getString("sNO");
        String assignment = jsonObject.getString("assignmentt");
        String report = jsonObject.getString("report");
        String textPart = jsonObject.getString("textPart");
        Integer paperWord = jsonObject.getInteger("wordSum");
        String abstract1 = jsonObject.getString("abstract");
        String checkAccept = jsonObject.getString("checkAndAccept");
        String isTruant = jsonObject.getString("isTruant");
        Integer truantDays = jsonObject.getInteger("truantDays");
        String isReply = jsonObject.getString("isReply");
        String remark = jsonObject.getString("remark");
        Integer grade1 = jsonObject.getInteger("grade1");
        Integer grade2 = jsonObject.getInteger("grade2");
        Integer grade3 = jsonObject.getInteger("grade3");
        Integer grade4 = jsonObject.getInteger("grade4");
        Integer grade5 = jsonObject.getInteger("grade5");
        Integer grade6 = jsonObject.getInteger("grade6");
        AdviserGrade adviserGrade =
                new AdviserGrade(null, sno, TNO, assignment, report, textPart, paperWord, abstract1, checkAccept,
                        isTruant, truantDays, isReply, remark, grade1, grade2, grade3, grade4, grade5, grade6, "审核中", null);
        Boolean flag = processService.addAdviserGrade(adviserGrade);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }

    /**
     * 指导老师提交一条中期检查
     */
    private void addMedium(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ProcessService processService = new ProcessServiceImp();
        String TNO = request.getParameter("userName");
        String mediumData = request.getParameter("mediumData");
        JSONObject jsonObject = JSONObject.parseObject(mediumData);
        String sno = jsonObject.getString("SNO");
        String isChange = jsonObject.getString("isChange");
        String changeReason = jsonObject.getString("ChangeReason");
        String isOne = jsonObject.getString("isOne");
        String isReport = jsonObject.getString("isReport");
        String isSurvey = jsonObject.getString("isSurvey");
        String problem = jsonObject.getString("ques");
        String plan = jsonObject.getString("plan");
        String lagReason = jsonObject.getString("plan-reason");
        String sManner = jsonObject.getString("stuManner");
        String tManner = jsonObject.getString("tManner");
        String quality = jsonObject.getString("quality");
        Medium medium =
                new Medium(sno, TNO, isChange, changeReason, isOne, isReport, isSurvey, problem, plan, lagReason, sManner, tManner, quality, null, null, "审核中");
        Boolean flag = processService.addMedium(medium);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }

    /**
     * 通过任务书ID获取任务书详情
     */
    private void getTaskById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ProcessService processService = new ProcessServiceImp();
        Integer id = Integer.parseInt(request.getParameter("id"));
        TaskDetail taskDetail = processService.getTaskDetailById(id);
        String json = JSONObject.toJSONString(taskDetail, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }

    /**
     * 根据论文的不同阶段查询论文
     */
    protected void getPaper(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ProcessService processService = new ProcessServiceImp();
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int offset = (page - 1) * limit;
        int type = Integer.parseInt(request.getParameter("type"));
        String state = request.getParameter("state");
        String tNO = request.getParameter("userName");
        String queryKey = request.getParameter("queryKey");
        if (queryKey == null || "".equals(queryKey)) {
            queryKey = "";
        }
        queryKey = "%" + queryKey + "%";
        MyTableData myTableData = processService.getAllPaper(queryKey, offset, limit, tNO, state, type);
        String json = JSONObject.toJSONString(myTableData, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }


    /**
     * 指导老师下达任务书
     */
    protected void addAssignment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ProcessService processService = new ProcessServiceImp();
        String TNO = request.getParameter("userName");
        String assData = request.getParameter("assData");
        JSONObject jsonData = JSONObject.parseObject(assData);
        String title = jsonData.get("title").toString();
        String collname = jsonData.get("college").toString();
        String mname = jsonData.get("major").toString();
        String sno = jsonData.get("SNO").toString();
        Task task = new Task(null, title, collname, mname, TNO, sno, "审核中", null, null);
        String starttime = jsonData.getString("myDate1");
        String require = jsonData.getString("text1");
        String paperNumber = jsonData.getString("text2");
        String matter = jsonData.getString("text3");
        String datum = jsonData.getString("text9");
        String executeTime = jsonData.getString("myDate7");
        String finishTime = jsonData.getString("myDate8");
        String comtent1 = jsonData.getString("text4");
        String comtent1Time = jsonData.getString("myDate2");
        String comtent2 = jsonData.getString("text5");
        String comtent2Time = jsonData.getString("myDate3");
        String comtent3 = jsonData.getString("text6");
        String comtent3Time = jsonData.getString("myDate4");
        String comtent4 = jsonData.getString("text7");
        String comtent4Time = jsonData.getString("myDate5");
        String comtent5 = jsonData.getString("text8");
        String comtent5Time = jsonData.getString("myDate6");
        TaskDetail taskDetail =
                new TaskDetail(null, starttime, require, paperNumber, matter, datum, executeTime, finishTime,
                        comtent1, comtent1Time, comtent2, comtent2Time, comtent3, comtent3Time, comtent4, comtent4Time, comtent5, comtent5Time);
        boolean flag = processService.addAssigmentByTNO(task, taskDetail);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }

}
